package view;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JScrollPane;
import javax.swing.JTable;

import types.ConsoleType;
import controller.Console;
import controller.Dayofweek;
import controller.TableModelAbsence;
import export.ExportOnDate;

@SuppressWarnings("serial")
public class PanelAbsenceList extends JPanel {

	private static PanelAbsenceList single;

	Date today = new Date();

	int buttonClicked = 0;

	JTable AbsenceList = new JTable(new TableModelAbsence());
	JScrollPane JSP = new JScrollPane(AbsenceList);

	private JButton addAB = new JButton("Nieuwe absentie");
	private JButton editAB = new JButton("Wijzig absentie");

	private JButton refreshAB = new JButton("Laat zien");
	private JButton JBexport = new JButton("Exporteer");
	private JButton setYear = new JButton("Schooljaar");
	private JButton setMonth = new JButton("Maand");
	private JButton setPreviousMonth = new JButton("Vorige maand");
	private JButton setWeek = new JButton("Week");
	private JButton setPreviousWeek = new JButton("Vorige week");

	private PanelAbsenceList() {

		setBounds(0, 0, 860, 720);
		setLayout(null);

		JSpinner spinnerStart = new JSpinner(new SpinnerDateModel(today, null,
				null, Calendar.MONTH));
		JSpinner.DateEditor dateEdit = new JSpinner.DateEditor(spinnerStart,
				"dd-MM-yyyy");
		spinnerStart.setEditor(dateEdit);

		JSpinner spinnerStop = new JSpinner(new SpinnerDateModel(today, null,
				null, Calendar.MONTH));
		JSpinner.DateEditor dateEdit2 = new JSpinner.DateEditor(spinnerStop,
				"dd-MM-yyyy");
		spinnerStop.setEditor(dateEdit2);

		spinnerStart.setBounds(280, 65, 140, 30);
		spinnerStart.setVisible(true);

		spinnerStop.setBounds(420, 65, 140, 30);
		spinnerStop.setVisible(true);

		refreshAB.setBounds(560, 65, 80, 30);
		refreshAB.setVisible(true);

		JBexport.setBounds(680, 65, 100, 30);

		AbsenceList.setRowSelectionInterval(0, 0);
		AbsenceList.removeColumn(AbsenceList.getColumnModel().getColumn(5));
		JSP.setBounds(280, 100, 500, 500);
		addAB.setBounds(1, 100, 140, 30);
		editAB.setBounds(1, 150, 140, 30);

		setWeek.setBounds(141, 250, 100, 20);
		setPreviousWeek.setBounds(141, 270, 100, 20);
		setMonth.setBounds(141, 290, 100, 20);
		setPreviousMonth.setBounds(141, 310, 100, 20);
		setYear.setBounds(141, 330, 100, 20);

		addAB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				PanelHolder mf = PanelHolder.getInstance();
				mf.remove(PanelAbsenceList.getInstance());
				mf.add(PanelAddAbsence.getInstance());
				PanelAddAbsence.getInstance().setpreviousPage(2);
				mf.repaint();
				mf.validate();
			}
		});
		editAB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

			}
		});
		setWeek.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				LocalDateTime monday = Dayofweek.getFirstDayTimeOfWeek();
				LocalDateTime sunday = Dayofweek.getLastDayTimeOfWeek();
				System.out.println(spinnerStart.getValue());
				System.out.println(monday);
				spinnerStart.setValue(monday);
				spinnerStop.setValue(sunday);
			}
		});

		refreshAB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				if (e.getActionCommand().equals("Laat zien")) {
					String message = " ";
					ConsoleType type = ConsoleType.SUCCESS;
					Date startDate = (Date) spinnerStart.getValue();
					Date endDate = (Date) spinnerStop.getValue();
					if (startDate.equals(endDate)) {
						message = " ";
						type = ConsoleType.SUCCESS;
					} else {
						if (startDate.after(endDate)
								|| endDate.before(startDate)) {
							message = "Controleer de datums";
							type = ConsoleType.ERROR;

						}
					}
					Console.getInstance().setConsole(message, type);
				}
			}
		});

		JBexport.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("Exporteer")) {
					// Get the start and end date from the spinner in yyyy/MM/dd
					System.out.println("ja het is export week");
					String stringStartDate = new SimpleDateFormat("yyyy/MM/dd")
							.format(spinnerStart.getValue());
					String stringEndDate = new SimpleDateFormat("yyyy/MM/dd")
							.format(spinnerStop.getValue());
					new ExportOnDate(stringStartDate, stringEndDate);
				}
			}
		});

		add(setWeek);
		add(setPreviousWeek);
		add(setMonth);
		add(setPreviousMonth);
		add(setYear);
		add(JSP);
		add(addAB);
		add(editAB);
		add(spinnerStart);
		add(spinnerStop);
		add(refreshAB);
		add(JBexport);

	}

	public static PanelAbsenceList getInstance() {
		if (single == null) {
			single = new PanelAbsenceList();
		}
		return single;
	}

}
